<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-DRC-double-info-widget" :style="getStyle('head')">
      <!--科室信息-->
      <div class="dept-room" :class="getDeptClassName"
           v-if="!(optionsInfo.deptHide)||!(optionsInfo.roomHide)">
        <p class="dept" v-if="!(optionsInfo.deptHide)">{{ getRoomDeptInfo.deptName || '\u00a0' }}</p>
        <p class="room" v-if="!(optionsInfo.roomHide)">
          {{ getRoomDeptInfo[optionsInfo['roomNameKey']] || getRoomDeptInfo.roomName || '\u00a0' }}
        </p>
      </div>
      <!--医生信息-->
      <div class="doctor-info flex-box"  v-for="item in getRoomDoctorInfo">
        <div class="picture flex-box-left">
          <img :src="(item['workerPicture']) || '/tpleditor/resource/assets/images/header.png'" alt="医生">
        </div>
        <div class="info flex-box-right">
          <div class="name-level">
            <p class="name"><ScrollX>{{ (item['workerName']) || '' }}</ScrollX></p>
            <p class="level" v-if="(item['workerTitle'])"><ScrollX>{{ (item.workerTitle) || '\u00a0' }}</ScrollX></p>
          </div>
          <!--患者信息-->
          <div class="patient-info" v-if="true">
            <div class="item current-patient flex-box" :class="getPatientClassName('current-patient')">
              <div class="title flex-box-left">就诊</div>
              <div class="content flex-box-right" :class="{'current-patient-info': scrollInfo(item)}">
                <template>
                  <div class="scroll-info" v-if="scrollInfo(item)">
                    <vue-seamless-scroll
                        class="scroll"
                        :ref="`scroll-${item.workerId}`"
                        @ScrollEnd="(e)=>handleScrollEnd(e, item)"
                        :class-option="optionScroll2"
                        :data="(item.treatingPatient)"
                    >

                      <template v-for="PatientItem in (item.treatingPatient)">
                        <p class="item">
                          {{ (PatientItem.orderNo) || '\u00a0' }}
                          {{ (PatientItem.patientName) || '\u00a0' }}
                        </p>
                      </template>
                    </vue-seamless-scroll>
                  </div>
                  <p v-else>
                    <template v-for="PatientItem in (item.treatingPatient)">
                      {{ (PatientItem.orderNo) || '\u00a0' }}
                      {{ (PatientItem.patientName) || '\u00a0' }}
                    </template>
                  </p>
                </template>
              </div>
            </div>
            <div class="item wait-patient flex-box" :class="getPatientClassName('wait-patient')">
              <div class="title flex-box-left">待诊</div>
              <div class="content flex-box-right">
                <p v-for="(item) in getWaitingPatientList(item.waitingPatientList)" :key="item.patientIdNo">
                  {{ item.orderNo || '\u00a0' }} {{ item.patientName || '\u00a0' }}
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import emitter from "@/utils/designer/emitter";
import {pxToVw} from '@/utils/common';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";
import {NoticeBar, Swipe, SwipeItem} from "vant";
import vueSeamlessScroll from 'vue-seamless-scroll';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
let buildParent = buildParentApi();
export default {
  name: "scene-DRC-double-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    SwipeItem,
    NoticeBar,
    Swipe,
    CompOperate,
    ScrollX,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      optionsInfo:Object(),
      header: require("@a/images/design/header.jpeg")
    }
  },

  computed: {
    optionScroll2() {
      return {
        hoverStop: true,
        direction: 1,                               // 0向下 1向上 2向左 3向右
        isSingleRemUnit: true,
        step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
        limitMoveNum: 2, // 开始无缝滚动的数据量 this.dataList.length
      };
    },

    // 判断当前患者是否滚动
    scrollInfo(){
      return function (info) {
        return !!(info.treatingPatient)&&(info.treatingPatient.length)>1
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
        }
      }
    },

    // 科室隐藏
    getDeptClassName(){
      let {deptHide} = (this.optionsInfo);
      if(deptHide){
        return 'dept-room-hide'
      }
    },

    // 设置不同模式下的样式
    getPatientClassName() {
      let waitMode = (this.optionsInfo)
      return function (type) {
        if (type === 'wait-patient') {
          switch (waitMode) {
            case 1:
              return 'current-patient'
            case 2:
              return 'current-patient2'
            case 3:
              return 'current-patient3'
          }
        } else if (type === 'wait-patient') {
          switch (waitMode) {
            case 1:
              return 'wait-patient'
            case 2:
              return 'wait-patient2'
            case 3:
              return 'wait-patient3'
          }
        }
      }
    },

    getDoctorClassName() {
      let {deptHide, roomHide} = (this.optionsInfo);
      if (deptHide && roomHide) {
        return 'doctor-info-2'
      } else if (deptHide || roomHide) {
        return 'doctor-info-1'
      } else {
        return 'doctor-info'
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDeptInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let temp = Array();
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      if (devType) {
        if (!!(roomDoctorInfo.apiData?.length)) {
          if ((roomDoctorInfo.apiData.length) >= 2) {
            return roomDoctorInfo.apiData.slice(0,2);
          } else {
            let arr = (new Array(2 - roomDoctorInfo.apiData.length).fill({}));
            return [...roomDoctorInfo.apiData, ...arr];
          }
        } else {
          return temp.push({},{});
        }
      } else {
        if (!!(roomDoctorInfo.mockData?.length)) {
          if ((roomDoctorInfo.mockData.length) >= 2) {
            return roomDoctorInfo.mockData.slice(0,2);
          } else {
            let arr = (new Array(2 - roomDoctorInfo.mockData.length).fill({}));
            return [...roomDoctorInfo.mockData, ...arr];
          }
        } else {
          return temp.push({},{});
        }
      }
    },

    // 候诊患者截取
    getWaitingPatientList() {
      return function (row) {
        if(!!row&&!!(row.length)){
          let {waitMode} = (this.optionsInfo);
          return row.slice(0, waitMode)
        }else {
          return [];
        }
      }
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          console.log(`科室信息-${data}`)
          this.states = true;
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-科室信息', key: key, data: data}))
          // this.parentApi.executeParentMethod('writeLog', JSON.stringify({sign:'模板日志-科室信息',key:key,data:data}));
          this.$store.dispatch('setRoomDeptInfo', data)
          break
        case "roomDoctorInfo":
          console.log(`全量医患信息-${data}`)
          this.states = true;
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-医生信息', key: key, data: data}))
          // this.parentApi.executeParentMethod('writeLog', JSON.stringify({sign:'模板日志-医生信息',key:key,data:data}));
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },
    // 滚动结束
    handleScrollEnd() {

    },
  },

  watch: {
    field: {
      handler: function (val) {
        let key = (val.options.config)
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-DRC-double-info-widget {
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  color: #333;
  height: 150px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;
  //科室
  .dept-room {
    padding: 20px 0 8px 0;
    background: #0059B3;
    height: 307px;
    border-radius: 20px;
    overflow: hidden;

    p {
      color: #FFFFFF;
      text-align: center;
      font-size: 80px;
      padding: 0 30px;
    }

    .dept {
      font-size: 80px;
      line-height: 120px;
      @include singleEllipsis(100%)
    }

    .room {
      font-size: 120px;
      font-weight: normal;
      line-height: 180px;
    }
  }

  .dept-room-hide{
    display: flex;
    p{
      width: 100%;
      align-self: center;
    }
  }

  //医生信息
  .doctor-info {
    display: flex;
    margin: 92px 0 0 0;
    min-width: auto;
    max-height: 526px;
    height: 526px;

    &:last-child {
      margin-top: 120px;

      .picture {
        order: 1;
      }

      .info {
        margin-right: 36px;
        .name-level{
          justify-content: end;
          .name{
            order: 1;
          }
          .level{
            margin-right: 40px;
          }
        }
      }

      .patient-info .item {
        .title {
          order: 1;
        }

        .content {
          text-align: right !important;

          p {
            text-align: left;
            display: flex;
            justify-content: left;
            padding-right: 46px;
            padding-left: 0;
          }
        }
      }
    }

    .picture {
      flex: inherit;
      border-radius: 20px;
      overflow: hidden;
      flex-basis: 398px;
      min-width: 398px;
      border: none;
      height: 536px;
      min-height: 536px;
      align-self: flex-end;
      background: transparent;

      img {
        width: 100%;
      }
    }

    .info {
      flex: inherit;
      display: flex;
      flex-direction: column;
      width: calc(100% - 398px - 34px);
      border: none;
      height: 100%;
      align-self: flex-end;
      background: transparent;

      .name-level {
        display: flex;
        margin-bottom: 30px;
        .name{
          font-size: 80px;
          line-height: 80px;
          height: 80px;
          flex-basis: 240px;
          max-width: 260px;
          @include fontWeight();
        }
        .level{
          font-size: 42px;
          color: #E8E8E8;
          flex-shrink: 1;
          white-space: nowrap;
          line-height: 72px;
          border-radius: 8px;
          padding: 4px 20px;
          overflow: hidden;
          margin-left: 30px;
          display: inline-block;
          background: #0059b3;
          position: relative;
          bottom: -12px;
        }

      }

      //患者信息
      .patient-info {
        .item {
          display: flex;
          overflow: hidden;
          border-radius: 12px;
          width: auto;
          min-width: auto;

          .title {
            color: white;
            align-items: center;
            align-self: center;
            text-align: center;
            padding: 0 30px;
            line-height: 64px;
            height: 100%;
            flex: inherit;
            display: flex;
            width: 124px !important;
            font-size: 58px;
            border: none;
          }

          .content {
            display: flex;
            align-items: center;
            flex-direction: column;
            align-self: center;
            justify-content: left;
            background: transparent;
            border: none;
            width: calc(100% - 124px) !important;

            p {
              font-size: 60px;
              padding-left: 60px;
              line-height: 86px;
              text-align: left;
              width: 100%;
            }
          }
        }

        .current-patient {
          height: 146px;
          min-height: 156px;
          background: rgba(0, 89, 179, 0.2);

          .title {
            background: rgba(0, 89, 179, 1);
          }

          p {
            color: rgba(0, 89, 179, 1);
          }
        }

        .current-patient-info {
          height: 100%;
          display: flex;
          align-self: center;
          align-items: center;

          .scroll-info {
            width: 100%;
            overflow: hidden;
            margin-top: 10px;
            height: calc(100% - 20px);
            box-sizing: border-box;

            p {
              height: 100%;
              width: 100%;
              display: flex;
              font-size: 60px;
              line-height: 1.5;
              align-items: flex-start;
              @include singleEllipsis(100%)
            }
          }
        }

        .wait-patient {
          height: 206px;
          margin-top: 44px;
          min-width: auto;
          background: rgba(255, 198, 85, 0.2);

          .title {
            background: rgba(229, 134, 2, 1);
          }

          p {
            color: rgba(229, 134, 2, 1);
            line-height: 106px !important;
            @include singleEllipsis(100%)
          }
        }

      }
    }
  }

  .doctor-info-1{
    margin: 118px 0 0 0;
  }

}
</style>
